package cn.yoyo.tools;

import cn.yoyo.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;


public final class MySession {

    private MySession(){

    }

    public static User getUser(){
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        Object object = authentication.getPrincipal();
        if (object instanceof User){
            return (User)object;
        }
        return null;
    }

    public static String getUserId(){
        User user = getUser();
        if (user == null){
            return null;
        }
        return user.getId();
    }

    public static String getUsername(){
        User user = getUser();
        if (user == null){
            return "";
        }
        return user.getUsername();
    }

    public static String getNickname(){
        User user = getUser();
        if (user == null){
            return "";
        }
        return user.getNickname();
    }

}
